package cn.itcast.bos.web.action.common;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

import org.apache.struts2.ServletActionContext;
import org.springframework.data.domain.Page;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import cn.itcast.bos.domain.base.Area;
import cn.itcast.bos.domain.base.Courier;

public class BaseAction<T> extends ActionSupport implements ModelDriven<T> {

	protected T model;

	@Override
	public T getModel() {
		return model;
	}

	// 泛型转换
	// 获取子类专递的真实类型,通过子类传递的真实类型对泛型entity进行实例化
	public BaseAction() {
		// 通过子类的实例获取父类的类型的对象BaseAction<T>
		Type type = this.getClass().getGenericSuperclass();
		// 强转成参数类型
		ParameterizedType parameterizedType = (ParameterizedType) type;
		Class entityClass = (Class) parameterizedType.getActualTypeArguments()[0];//0表示第一个参数,也就是对应泛型中的Area
		try {
			model = (T)entityClass.newInstance();//new Area();	
		} catch (InstantiationException  | IllegalAccessException e) {
			e.printStackTrace();
		} 
	}
	
	protected int page;
	protected int rows;

	public void setPage(int page) {
		this.page = page;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}
	//将分页结果放到栈顶
	protected void pushPageDataToValueStack(Page<T> page2) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("total", page2.getTotalElements());
		map.put("rows", page2.getContent());
		ServletActionContext.getContext().getValueStack().push(map);
		
	}

}









